<?php namespace Wx\Controller;

use Hdphp\Controller\Controller;


class LibraryController extends CommonController
{
  //GET /photo 索引
  public function index()
  {
    if(IS_POST)
    {
      // var_dump($_FILES);die;
      if (Api('Library/addImg'))
      {
        $this->success('添加成功！');
      }
      else
      {
        $this->error(Api('Library/getError'));
      }
    }
    //分配导航条参数
    $w = Q('w',0,'intval');
    $this->assign('w',$w);
    switch ($w) {
      case 0:
      //分配文字素材
      $word = Api('Library/getWordList');
      $this->assign('word',$word);
        break;
      case 1:
      //分配图文素材
      $word = Api('Library/getWordList');
      $this->assign('word',$word);
        break;
      default:
      //分配图片素材
      $img = Api('Library/getImgList');
      $this->assign('img',$img);
        break;
    }
    //分配图片素材
    // var_dump($img);die;
    $this->display();
  }

  //新建文字消息
  public function create_word()
  {
    // echo 123;die;
    $w = Q('w',0,'intval');
    $this->assign('w',$w);
    $this->display();
  }

  //编辑文字消息
  public function edit_word()
  {
    // echo 123;die;
    $id = $_GET['id'];
    $word = Api('Library/getOldWord',array($id));
    $w = Q('w',0,'intval');
    // var_dump($word);die;
    $this->assign('w',$w);
    $this->assign('word',$word);
    $this->display();
  }

  // post方式新增图文消息
  public function create_wordimg()
  {
    if(IS_POST)
    {
      // var_dump($_FILES);die;
        if (Api('Library/addWordImg'))
        {
          $this->success('添加成功！');
        }
        else
        {
          $this->error(Api('Library/getError'));
        }
    }

    $w = Q('w',0,'intval');
    $this->assign('w',$w);
    $this->display();
  }

  //DELETE /photo/{photo} 删除文字素材
  public function destroy($id)
  {
    if (Api('Library/destroy',array($id))) 
    {
      $data = array(
        'status' => 1,
        'content' => '删除成功！',
      );
    }
    else
    {
      $data = array(
        'status' => 0,
        'content' => '删除失败！'
      );
    }
    $this->ajax($data);
  }

  //删除图片素材
  public function delImg()
  {
    if (Api('Library/delImg')) 
    {
      $data = array(
        'status' => 1,
        'content' => '删除成功！',
      );
    }
    else
    {
      $data = array(
        'status' => 0,
        'content' => '删除失败！'
      );
    }
    $this->ajax($data);
  }


  //ajax新增封面图片
  public function uploadify()
  {
    /*$data = Api('Library/uploadify');
    $this->ajax($finalpath);*/
    $re = Api('Library/uploadify');
    if ($re) 
    {
      $data = $re;
    }
    else
    {
      $data = array(
        'status' => 0,
        'content' => '添加失败！'
      );
    }
    $this->ajax($data);
  }


  //删除父级分类以及其下的子分类
  public function delAll()
  {
    
  }


  //POST /photo 保存新增文字消息
  public function store()
  {
    if (Api('Library/store')) {
      // var_dump(23);die;
      $data = array(
        'status' => 1,
        'content' => '添加成功！',
      );
    }
    else
    {
      $data = array(
        'status' => 0,
        'content' => Api('Library/getError')
      );
    }
    $this->ajax($data);die;
    // var_dump($_POST);die;
  }

 /* //保存新增图片素材
  public function storeImg()
  {
    if (Api('Library/storeImg')) {
      // var_dump(23);die;
      $data = array(
        'status' => 1,
        'content' => '添加成功！',
      );
    }
    else
    {
      $data = array(
        'status' => 0,
        'content' => Api('Library/getError')
      );
    }
    $this->ajax($data);die;
  }*/

  //保存新增图文消息
  public function storeWordImg()
  {
    if (Api('Library/storeWordImg')) {
      // var_dump(23);die;
      $data = array(
        'status' => 1,
        'content' => '添加成功！',
      );
    }
    else
    {
      $data = array(
        'status' => 0,
        'content' => Api('Library/getError')
      );
    }
    $this->ajax($data);die;
    // var_dump($_POST);die;
  }

  //PUT /photo/{photo} 更新数据
  public function update($id) 
  {
    if(Api('Library/update',array($id)))
    {
      $data['status'] = 1;
      $data['content'] = '更新成功！';
    }
    else
    {
      $data['status'] = 0;
      $data['content'] = Api('Library/getError');
    }
    $this->ajax($data);
  }

  //生成
  public function push()
  {
    
  }

  //生成
  public function revoke()
  {
    
  }


  //GET /photo/create 创建界面
  public function create()
  {

  }


  //GET /photo/{photo} 显示文章
  public function show()
  {

  }

  //GET /photo/{photo}/edit 更新界面
  public function edit($id)
  {
    
  }


  

}